home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 12 / Amiga Format AFCD12 (Apr 1997, Issue 96).iso / -in_the_mag- / html_tutorial / mas_rec.cpp < prev    next >
C/C++ Source or Header  |  1997-01-21  |  3KB  |  122 lines

  1. // Record details about who the viewer of a web page is
  2. //
  3. // (C) M.A.Smith University of Brighton
  4. //
  5. // Permission is granted to use this code
  6. //   provided this declaration and copyright notice remains intact.
  7. //
  8. // 26 August 1995
  9.  
  10.  
  11. #include <iostream.h>
  12. #include <fstream.h>
  13. #include <iomanip.h>
  14.  
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19.  
  20. #include "parse.h"
  21. #include "parse.cpp"
  22.  
  23. void write_log_entry( char[], char[] );
  24. void cgi_var_output();
  25. char* getenv_n( char [] );
  26. void gif_output( char [] );
  27.  
  28. // Parameters to the CGI program
  29. //  passed in the QUERY_STRING environment variable
  30. //
  31. //  file=mame  - Names the file to which log information is written
  32. //  page=name  - The name of the page that will be written to the log
  33. //  img=name   - Names the image file which is returned
  34. //             - If no name or unreadable an image of a . is returned
  35. //             - If this parameter is omitted then a web page of CGI
  36. //             -  variables is returned to help in debuging
  37.  
  38. // Remember all files name must be absolute or relative to the
  39. //  directory in which the CGI script is run
  40.  
  41. inline void html( char str[] ) { cout << str << "\n"; }
  42.  
  43. inline void html_( char str[] ) { cout << str; }
  44.  
  45. inline void html_( char c ) { cout << c; }
  46.  
  47.  
  48. int main()
  49. {
  50.   char *query_str = getenv("QUERY_STRING");
  51.   Parse list( query_str == 0 ? 
  52.           "file=mas&page=test&img=" : query_str );
  53.  
  54.   if ( list.get_item( "file" ) != NULL )
  55.   {
  56.     write_log_entry( list.get_item( "file", 1, true ),
  57.              list.get_item( "page" ) );
  58.   }
  59.   if ( list.get_item( "img" ) != NULL )
  60.   {
  61.     gif_output( list.get_item("img", 1, true) );
  62.   } else {
  63.     html("Content-type: text/html"); html("");
  64.     cgi_var_output();      // debug option
  65.   }
  66.   return 0;
  67. }
  68.  
  69.  
  70. void write_log_entry( char file[], char page[] )
  71. {
  72.   ofstream inf( file, ios::app );
  73.   if ( !inf.fail() )
  74.   {
  75.     time_t t;
  76.     time( &t);
  77.     char *str = ctime( &t ); str[24] = '\0';
  78.     inf << setiosflags( ios::left );
  79.     inf << setw(24) << str << " " <<
  80.        setw(10)  << (page!=NULL? page : "Unknown" ) <<
  81.        setw(20) << getenv_n("REMOTE_HOST") <<
  82.        setw(20) << getenv_n("REMOTE_ADDR") << "\n";
  83.     //       setw(20) << getenv_n("REMOTE_USER") << "\n";
  84.   }
  85. }
  86.  
  87.  
  88. void gif_output( char gif[] )
  89. {
  90.   unsigned char square [] = {
  91.     'G',  'I',  'F',  '8',  '9',  'a', 
  92.     0002, 0000, 0002, 0000, 0263, 0000, 0000, 0000, 0000, 0000,
  93.     0277, 0000, 0000, 0000, 0277, 0000, 0277, 0277,
  94.     0000, 0000, 0000, 0277, 0277, 0000, 0277, 0000,
  95.     0277, 0277, 0300, 0300, 0300, 0200, 0200, 0200,
  96.     0377, 0000, 0000, 0000, 0377, 0000, 0377, 0377,
  97.     0000, 0000, 0000, 0377, 0377, 0000, 0377, 0000,
  98.     0377, 0377, 0377, 0377, 0377, 0054, 0000, 0000,
  99.     0000, 0000, 0002, 0000, 0002, 0000, 0100, 0004,
  100.     0003, 0020, 0200, 0010, 0000, 0073 };
  101.  
  102.   html("Content-type: image/gif"); html("");
  103.  
  104.   if ( gif[0] != '\0' )            // Output gif file
  105.   {
  106.     ifstream in( gif );
  107.     if ( !in.fail() )              // Can read
  108.     { 
  109.       char c;
  110.       in.read( &c, 1 );
  111.       while ( !in.eof() )
  112.       {
  113.      cout.write( &c, 1 );
  114.      in.read( &c, 1 );
  115.       }
  116.       return;                      // Finish
  117.     }
  118.   }
  119.   cout.write( square, sizeof( square ) );
  120. }
  121.  
  122.